﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyAlbum.Model;
using MyAlbum.Utility;
using MyAlbum.DataAccess.LinqToSql;


namespace MyAlbum.Data.Linq
{
    public class SupportRepository : IRepository<MyAlbum.Model.Support>
    {

        private string _connectionString;
        public SupportRepository(string connectionString)
        {
            Assert.IsNotNull(connectionString);
            this._connectionString = connectionString;
        }
        #region IRepository<Section> Members

        public List<MyAlbum.Model.Support> GetItems()
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.SupportRequests.Select(s =>
                    new MyAlbum.Model.Support()
                    {
                        Id = s.Id.ToString(),
                        Name = s.Name,
                        Body = s.Body,
                        Email = s.Email,
                        IsActive = s.Active,
                        Subject = s.Subject,
                        TimeStamp = s.Timestamp
                    }).ToList();
            }
        }

        public void AddItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            SupportRequest s = new SupportRequest()
            {
                 Id=Guid.NewGuid(),
                Name = item.Name,
                Body = item.Body,
                Timestamp = item.TimeStamp,
                Subject = item.Subject,
                Active = item.IsActive,
                Email = item.Email
            };
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                ImagesDB.SupportRequests.InsertOnSubmit(s);
                ImagesDB.SubmitChanges();
            }
            item.Id = s.Id.ToString();
        }

        public void DeleteItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.SupportRequests.FirstOrDefault(s => s.Id ==new Guid( item.Id));
                if (old != null)
                {
                    ImagesDB.SupportRequests.DeleteOnSubmit(old);
                    ImagesDB.SubmitChanges();
                }
            }
        }

        public void UpdateItem(MyAlbum.Model.Support item)
        {
            Assert.IsNotNull(item);
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                var old = ImagesDB.SupportRequests.FirstOrDefault(s => s.Id == new Guid(item.Id));
                if (old != null)
                {
                    old.Email = item.Email;
                    old.Body = item.Body;
                    old.Active = item.IsActive;
                    old.Name = item.Name;
                    old.Subject = item.Subject;
                    ImagesDB.SubmitChanges();
                }
            }
        }

        public MyAlbum.Model.Support GetItemById(string id)
        {
            using (var ImagesDB = new DataClasses1DataContext(this._connectionString))
            {
                return ImagesDB.SupportRequests.Where(s => s.Id ==new Guid( id)).Select(s =>
                    new MyAlbum.Model.Support()
                    {
                        Id = s.Id.ToString(),
                        Name = s.Name,
                        Body = s.Body,
                        Email = s.Email,
                        IsActive = s.Active,
                        Subject = s.Subject,
                        TimeStamp = s.Timestamp
                    }).SingleOrDefault();
            }
        }
        #endregion
    }
}
